<?php
declare (strict_types = 1);

namespace app\command;

use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;
use think\facade\Config;
use think\facade\Db;

class MakeModel extends Command
{
    protected function configure()
    {
		$this->setName('make_model')
			->setDescription('自动生成数据库表模型文件到 app/model 目录下');
    }

    protected function execute(Input $input, Output $output)
    {
		$tables = Db::query("SHOW TABLES");
		foreach ($tables as $table) {
			$tableName = $table['Tables_in_'.env('DB_NAME')];
			$modelName = str_replace('_', '', ucwords($tableName, '_'));
			$modelPath = app_path().'model/'.$modelName.'.php';
			if (!is_dir(app_path().'model/')) {
				mkdir(app_path().'model/', 0777, true);
			}
			if (!file_exists($modelPath)) {
				$modelContent = <<<EOF
<?php
declare (strict_types = 1);

namespace app\model;

use think\Model;

/**
 * @mixin \\think\Model
 */
class {$modelName} extends Model
{
    //
}
EOF;
				file_put_contents($modelPath, $modelContent);
			}
		}
        // 指令输出
        //$output->writeln('app\command\makemodel');
    }
}
